package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zzb
 * @create 2021/2/12
 */
@RestController
@RequestMapping("label")
@CrossOrigin
public class LabelController {
    @Autowired
    private LabelService labelService;

    //添加lable
    @PostMapping
    public Result add(@RequestBody Label label){
        try {
            labelService.add(label);
            System.out.println("label = " + label);
            return new Result(true, StatusCode.OK,"增加标签成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"添加标签失败");
        }
    }

    //查询全部标签
    @GetMapping
    public Result findAll(){
        try {
            //查询所有标签
            List<Label> labelList = labelService.findAll();
//        System.out.println("labelList = " + labelList);
            //返回
            return new Result(true,StatusCode.OK,"查询所有标签成功",labelList);
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    //根据ID查询标签
    @GetMapping("{labelId}")
    public Result findById(@PathVariable String labelId){
        try {
            Label label = labelService.findById(labelId);
            return new Result(true,StatusCode.OK,"根据id查询标签成功",label);
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    //根据id修改标签
    @PutMapping("{labelId}")
    public Result updateById(@PathVariable String labelId,@RequestBody Label label){
        //将标签id与对象绑定
        label.setId(labelId);
        //修改标签
        try {
            labelService.updateById(label);
            return new Result(true,StatusCode.OK,"修改标签成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"修改标签失败");
        }
    }

    //根据id删除标签
    @DeleteMapping("{labelId}")
    public Result deleteById(@PathVariable String labelId){
        try {
            labelService.deleteById(labelId);
            return new Result(true,StatusCode.OK,"根据id删除标签成功");
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"根据id删除标签失败");
        }
    }

    //进行标签的条件查询
    @PostMapping("search")
    public Result search(@RequestBody Label label){
        try {
            //根据条件查询标签集合
            List<Label> label1 = labelService.search(label);
            return new Result(true,StatusCode.OK,"查询成功",label1);
        } catch (Exception e) {
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    //标签分页
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Label label,@PathVariable Integer page,@PathVariable Integer size){
        PageResult<Label> labels = labelService.search(label,page,size);
        return new Result(true,StatusCode.OK,"分页查询成功",labels);
    }
}
